package com.byit.solr.impl;

import com.byit.domain.AppApplication;
import com.byit.domain.AppApplictionMenu;
import com.byit.mapper.AppApplicationMapper;
import com.byit.mapper.AppApplictionMenuMapper;
import com.byit.solr.ISolrService;
import com.byit.solr.SolrResult;
import com.byit.utils.StringUtil;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * solr服务类
 *
 * @author tjm
 * @date 2019/1/11.
 */
@Service
public class SolrApplicationMenuService implements ISolrService<AppApplictionMenu> {

    @Autowired
    private SolrClient client;
    @Autowired
    private AppApplictionMenuMapper appApplictionMenuMapper;

    private String core = "app_menu_core";


    @Override
    public void addOrUpdate(AppApplictionMenu appApplictionMenu) {
        SolrInputDocument doc = new SolrInputDocument();
        appApplictionMenu = appApplictionMenuMapper.selectById(appApplictionMenu.getId());
        doc.setField("id", appApplictionMenu.getId());
        doc.setField("appId", appApplictionMenu.getAppId());
        doc.setField("name", appApplictionMenu.getName());
        doc.setField("url", appApplictionMenu.getUrl());
        doc.setField("accessType", appApplictionMenu.getAccessType());
        doc.setField("logo",appApplictionMenu.getLogo());
        doc.setField("sort",appApplictionMenu.getSort());
        try {
            client.add(core,doc);
            client.commit(core);
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void delete(String ids) {
        try {
            List<String> idList = StringUtil.splitStr(ids, ",");
            client.deleteById(core,idList);
            client.commit(core);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public SolrResult select(JSONObject param) {
        try {
            SolrQuery params = new SolrQuery();
            // 查询条件
            params.setQuery("name:"+param.getString("title"));
//        // 排序
//            params.addSort("sort", SolrQuery.ORDER.asc);
//        // 分页
            params.setStart(param.has("page") ? param.getInt("page")-1 : PAGE);
            params.setRows(param.has("limit") ? param.getInt("limit") : LIMIT);
//
//        // 默认域
            params.set("df", "id,name,accessType,logo,url,sort,appId");
//
//        // 只查询指定域
            params.set("fl", "id,name,accessType,logo,url,sort,appId");

            // 开启高亮
            params.setHighlight(true);
            // 高亮字段
            String hight = "name";
            params.addHighlightField(hight);
            //标记，高亮关键字前缀
            params.setHighlightSimplePre("<font color='red'>");
            //后缀
            params.setHighlightSimplePost("</font>");
            // solr数据库是 itaem
            QueryResponse queryResponse = client.query(core,params);
          return SolrResult.result(queryResponse,params.getRows(),hight);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
